<?php
namespace app\custom_user\model;

use think\Model;

/**
 * 用户模型
 */
class UserModel extends Model
{
    protected $table="ue_user";
    public function encryptPassword($password,$time)
    {
        $time=$time%10000;//截取时间戳的后四位
        $encryptString=$time.$password;
        $encrypted=sha1($encryptString);
        return $encrypted;
    }
    /**
     * 正常的创建用户
     * 检查账号和手机号是否被占用
     */
    public function createUser($data)
    {
        //验证能否注册
        $record=$this->where(["username"=>$data["username"]])->find();
        if($record){
            $this->error="账号已经被占用";
            return false;
        }
        $create_time=time();
        $data["last_login_time"]=$create_time;
        $data["last_login_ip"]=get_client_ip();
        $data["token"]=UserSession::token();
        $data["password"]=$this->encryptPassword($data["password"],$create_time);
        $data["create_time"]=$create_time;
        $result=$this->except(["id","score","coin","status"])->isUpdate(false)->save($data);
        if($result===false){
            $this->error="保存数据失败";
            return false;
        }
        return $this->where(["id"=>$this["id"]])->find()->getData();
    }
    /**
     * 登录
     */
    public function loginUser($user)
    {
        $data=[
            "last_login_time"=>time(),
            "last_login_ip"=>get_client_ip(),
            "token"=>UserSession::token()
        ];
        $result = $this->isUpdate(true)->save($data,["id"=>$user["id"]]);
        if($result===false){
            $this->error="登录失败";
            return false;
        }
        return true;
    }
    /**
     * 检查用户和密码
     */
    public function checkUser($where,$password)
    {
        $user=$this->where($where)->find();
        if(!$user){
            $this->error="用户不存在";
            return false;
        }
        $password=$this->encryptPassword($password,$user["create_time"]);
        if($user["password"]!==$password){
            $this->error="密码错误";
            return false;
        }
        return $user->getData();
    }
    /**
     * 修改用户简介
     */
    public function updateProfile($id,$data)
    {
        $result = $this->allowField(["nickname","avatar","signature","sex","birthday","url"])->isUpdate(true)->save($data,["id"=>$id]);
        if($result===false){
            $this->error="修改用户简介失败";
            return false;
        }
        return true;
    }
    public function updateMobile($id,$mobile)
    {
        if($this->where(["mobile"=>$mobile])->find()){
            $this->error="手机号已被占用";
            return false;
        }
        return $this->isUpdate(true)->where(["id"=>$id])->setField("mobile",$mobile);
    }
    public function updatePassword($user,$old,$new)
    {
        $create_time=$user["create_time"];
        if($user["password"]!==$this->encryptPassword($old,$create_time)){
            $this->error="原密码错误";
            return false;
        }
        $password=$this->encryptPassword($new,$create_time);
        return $this->isUpdate(true)->where(["id"=>$user["id"]])->setField("password",$password);
    }
}
